﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AAA.Chart.Data;
using AAA.Forms.Components.Util;
using AAA.Chart.Indicator;

namespace AAA.Chart.Component
{
    public partial class ChartPane : UserControl
    {
        private AAA.Meta.Chart.MoueClickEvent _mouseClickEvent;
        private AAA.Meta.Chart.MoueDoubleClickEvent _mouseDoubleClickEvent;
        private AAA.Meta.Chart.MoueMoveEvent _mouseMoveEvent;

        private Dictionary<DateTime, List<ExtraInfo>> _dicExtraInfo;

        public ChartPane()
        {
            InitializeComponent();

            InitTable();

            _mouseClickEvent = new AAA.Meta.Chart.MoueClickEvent(OnMouseClick);
            _mouseDoubleClickEvent = new AAA.Meta.Chart.MoueDoubleClickEvent(OnMouseDoubleClick);
            _mouseMoveEvent = new AAA.Meta.Chart.MoueMoveEvent(OnMouseMove);
            _dicExtraInfo = new Dictionary<DateTime, List<ExtraInfo>>();
        }

        public void AddExtraInfo(DateTime dtBarTime, ExtraInfo extraInfo)
        {
            if (_dicExtraInfo.ContainsKey(dtBarTime) == false)
                _dicExtraInfo.Add(dtBarTime, new List<ExtraInfo>());

            List<ExtraInfo> lstExtraInfo = _dicExtraInfo[dtBarTime];

            for(int i = 0; i < lstExtraInfo.Count; i++)
                if (lstExtraInfo[i].Item == extraInfo.Item)
                {
                    lstExtraInfo[i] = extraInfo;
                    return;
                }
            lstExtraInfo.Add(extraInfo);
        }

        public void ClearExtraInfo()
        {
            _dicExtraInfo = new Dictionary<DateTime, List<ExtraInfo>>();
        }

        private void InitTable()
        {
            tblData.Columns.Add("Item", "項目");
            tblData.Columns.Add("Value", "內容");
        }

        public void InitChartEvent()
        {
            foreach (ChartX chart in ccChartContainer.GetAllCharts())
            {                
                chart.OnMouseClick -= _mouseClickEvent;
                chart.OnMouseDoubleClick -= _mouseDoubleClickEvent;
                chart.OnMouseMove -= _mouseMoveEvent;

                chart.OnMouseClick += _mouseClickEvent;
                chart.OnMouseDoubleClick += _mouseDoubleClickEvent;
                chart.OnMouseMove += _mouseMoveEvent;
            }
        }

        void OnMouseMove(AAA.Meta.Chart.Data.PointData pointData)
        {
            int iXPoint = (int)pointData.XValue;
            DateTime dtBarTime = DateTime.MinValue;

            if (iXPoint < 0)
                return;

            DataGridViewUtil.Clear(tblData);
            foreach (ChartX chart in ccChartContainer.GetAllCharts())
            {
                foreach (IIndicator indicator in chart.GetIndicators())
                {                    
                    dtBarTime = indicator.DataSource.GetData(indicator.BaseStudy).DateTime(iXPoint);

                    if (DataGridViewUtil.FindRowIndex(tblData,
                                                     new string[] { "Item" },
                                                     new object[] { "時間" }) < 0)
                    {
                        DataGridViewUtil.InsertRow(tblData,
                                                   new object[] { "時間", dtBarTime.ToString("yyyy/MM/dd HH:mm:ss") });
                    }
                    else
                    {
                        DataGridViewUtil.UpdateRow(tblData,
                                                   new string[] { "Item" },
                                                   new object[] { "時間" },
                                                   new object[] { "時間", dtBarTime.ToString("yyyy/MM/dd HH:mm:ss") });
                    }
                    switch (indicator.DataField)
                    {
                        case AAA.Meta.Chart.DataTypeEnum.CandleStick:
                            if (DataGridViewUtil.FindRowIndex(tblData,
                                                     new string[] { "Item" },
                                                     new object[] { "開盤價" }) < 0)
                            {
                                DataGridViewUtil.InsertRow(tblData,
                                                           new object[] { "開盤價", indicator.DataSource.GetData(indicator.BaseStudy).Open(iXPoint) });
                            }
                            else
                            {
                                DataGridViewUtil.UpdateRow(tblData,
                                                           new string[] { "Item" },
                                                           new object[] { "開盤價" },
                                                           new object[] { "開盤價", indicator.DataSource.GetData(indicator.BaseStudy).Open(iXPoint) });
                            }
                            if (DataGridViewUtil.FindRowIndex(tblData,
                                                     new string[] { "Item" },
                                                     new object[] { "最高價" }) < 0)
                            {
                                DataGridViewUtil.InsertRow(tblData,
                                                           new object[] { "最高價", indicator.DataSource.GetData(indicator.BaseStudy).High(iXPoint) });
                            }
                            else
                            {
                                DataGridViewUtil.UpdateRow(tblData,
                                                           new string[] { "Item" },
                                                           new object[] { "最高價" },
                                                           new object[] { "最高價", indicator.DataSource.GetData(indicator.BaseStudy).High(iXPoint) });
                            }
                            if (DataGridViewUtil.FindRowIndex(tblData,
                                                     new string[] { "Item" },
                                                     new object[] { "最低價" }) < 0)
                            {
                                DataGridViewUtil.InsertRow(tblData,
                                                           new object[] { "最低價", indicator.DataSource.GetData(indicator.BaseStudy).Low(iXPoint) });
                            }
                            else
                            {
                                DataGridViewUtil.UpdateRow(tblData,
                                                           new string[] { "Item" },
                                                           new object[] { "最低價" },
                                                           new object[] { "最低價", indicator.DataSource.GetData(indicator.BaseStudy).Low(iXPoint) });
                            }
                            if (DataGridViewUtil.FindRowIndex(tblData,
                                                     new string[] { "Item" },
                                                     new object[] { "收盤價" }) < 0)
                            {
                                DataGridViewUtil.InsertRow(tblData,
                                                           new object[] { "收盤價", indicator.DataSource.GetData(indicator.BaseStudy).Close(iXPoint) });
                            }
                            else
                            {
                                DataGridViewUtil.UpdateRow(tblData,
                                                           new string[] { "Item" },
                                                           new object[] { "收盤價" },
                                                           new object[] { "收盤價", indicator.DataSource.GetData(indicator.BaseStudy).Close(iXPoint) });
                            }
                            break;
                        case AAA.Meta.Chart.DataTypeEnum.Close:
                            if (DataGridViewUtil.FindRowIndex(tblData,
                                                     new string[] { "Item" },
                                                     new object[] { indicator.Name }) < 0)
                            {
                                DataGridViewUtil.InsertRow(tblData,
                                                           new object[] { indicator.DataSource.GetData(indicator.BaseStudy).Close(iXPoint) });
                            }
                            else
                            {
                                DataGridViewUtil.UpdateRow(tblData,
                                                   new string[] { "Item" },
                                                   new object[] { indicator.Name },
                                                   new object[] { indicator.Name, indicator.DataSource.GetData(indicator.BaseStudy).Close(iXPoint) });
                            }
                            break;
                        case AAA.Meta.Chart.DataTypeEnum.Volume:
                            if (DataGridViewUtil.FindRowIndex(tblData,
                                                     new string[] { "Item" },
                                                     new object[] { "成交量" }) < 0)
                            {
                                DataGridViewUtil.InsertRow(tblData,
                                                           new object[] { "成交量", indicator.DataSource.GetData(indicator.BaseStudy).Volume(iXPoint) });
                            }
                            else
                            {
                                DataGridViewUtil.UpdateRow(tblData,
                                                           new string[] { "Item" },
                                                           new object[] { "成交量" },
                                                           new object[] { "成交量", indicator.DataSource.GetData(indicator.BaseStudy).Volume(iXPoint) });
                            }
                            break;
                    }
                }              
            }
            if (_dicExtraInfo.ContainsKey(dtBarTime) == false)
                return;

            List<ExtraInfo> lstExtraInfo = _dicExtraInfo[dtBarTime];

            for (int i = 0; i < lstExtraInfo.Count; i++)
            {
                if (DataGridViewUtil.FindRowIndex(tblData,
                                             new string[] { "Item" },
                                             new object[] { lstExtraInfo[i].Item }) < 0)
                {
                    DataGridViewUtil.InsertRow(tblData,
                                               new object[] { lstExtraInfo[i].Item, lstExtraInfo[i].Value });
                }
                else
                {
                    DataGridViewUtil.UpdateRow(tblData,
                                               new string[] { "Item" },
                                               new object[] { lstExtraInfo[i].Item },
                                               new object[] { lstExtraInfo[i].Item, lstExtraInfo[i].Value });
                }
            }

        }

        void OnMouseClick(AAA.Meta.Chart.Data.PointData pointData)
        {
            
        }

        void OnMouseDoubleClick(AAA.Meta.Chart.Data.PointData pointData)
        {
            
        }

        public ChartContainer ChartContainer
        {
            get
            {
                return ccChartContainer;
            }
        }

        public DataGridView Table
        {
            get
            {
                return tblData;
            }
        }

        

    }
}
